import { createRouter, createWebHistory } from 'vue-router'
import RoomLobby from '../components/RoomLobby.vue'
import GameRoom from '../views/GameRoom.vue'

const routes = [
  {
    path: '/',
    name: 'Lobby',
    component: RoomLobby,
    meta: {
      title: '房间大厅 - 涂色抢地盘游戏'
    }
  },
  {
    path: '/room/:roomCode',
    name: 'GameRoom',
    component: GameRoom,
    meta: {
      title: '游戏房间 - 涂色抢地盘游戏'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue'),
    meta: {
      title: '页面未找到 - 涂色抢地盘游戏'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }

  next()
})

export default router
